package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tb.v1.api.shoop.ISpOrderService;
import com.tb.v1.api.vo.SpOrderVo;
import com.tb.v1.entity.SpOrder;
import com.tb.v1.result.ResultJson;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/29 0029 10:07
 * @Description:
 */
@RestController
@RequestMapping("/v1")
public class SpOrderController extends BaseController {
    @Reference
    private ISpOrderService spOrderService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description
     * @Date 2020/7/29 0029 10:09
     * @Param []
     **/
    @RequestMapping("orders")
    public ResultJson<Object> getOrderList(SpOrderVo vo) {
        Map map = new HashMap();
        Integer page = (vo.getPageIndex() - 1) * vo.getPageSize();
        map.put("page", page);
        map.put("size", vo.getPageSize());
        map.put("orderNum", vo.getQuery());
        List<SpOrder> orders = spOrderService.getOrderList(map);
        Integer totalNum = 0;
        if (!orders.isEmpty()) {
            totalNum = spOrderService.getOrderListCount(map);
        }
        Map resultMap = new HashMap();
        resultMap.put("total", totalNum);
        resultMap.put("pageIndex", vo.getPageIndex());
        resultMap.put("orders", orders);
        return toResultModel("200", "成功", resultMap);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 修改地址信息
     * @Date 2020/7/29 0029 15:34
     * @Param [vo]
     **/
    @RequestMapping("update/address/{id}")
    public ResultJson<Object> getUpdateOrder(@PathVariable("id") String id,
                                             @RequestParam("address1") String address1,
                                             @RequestParam("address2") String address2) {
        SpOrder spOrder = new SpOrder();
        spOrder.setOrderId(Integer.valueOf(id));
        spOrder.setConsigneeAddr(address1 + " " + address2);
        Integer num = spOrderService.updateEntity(spOrder);
        if (num > 0) {
            return toResultModel("200", "修改成功");
        } else {
            return toResultModel("200", "修改失败");
        }
    }
}
